<?php

require_once('./modules/pages/loader.inc');
require_once('./modules/util.inc');

class EditPageRenderer extends Renderer
{
	protected $_pageId;
	protected $_success;
	protected $_fileInfo;
	
	
	function pageLoad()
	{
		$this->_pageId = $_GET['pageId'];
		
		if(!isset($this->_pageId) || $this->_pageId == "")
			$this->_pageId = "index";
			
		$this->_fileInfo = PagesLoader::loadPage($this->_pageId);

		switch( $this->action )
		{
			case "save":
				$this->savePage();
				break;
			case "":
			case "show":
			default:
				break;
		}
	}

	function savePage()
	{
		//freeRTE throws 2 calls for some reason...
		if ( $_SERVER['REQUEST_METHOD'] == "GET")
			return;
			
		$this->_success = false;
			
		$this->_fileInfo['content'] = $content = html_entity_decode(html_entity_decode(Util::safeGetPost('txtPageContent'),ENT_QUOTES),ENT_QUOTES);
		$this->_fileInfo['title'] = $title = Util::safeGetPost('txtTitle');
		$this->_fileInfo['desc'] = $desc = Util::safeGetPost('txtDesc');
		$this->_fileInfo['name'] = $name = Util::safeGetPost('txtName');
		$showInMenu = isset($_POST['chkShowInMenu'])? $_POST['chkShowInMenu'] : false;
		
		$this->_success = PagesLoader::savePage($this->_pageId,$content,$title,$desc,$name,$showInMenu,$this->_fileInfo['position'],true);
	}
	
	function isPermitted()
	{
		//echo "permission check feeds";
		if ( isset($this->user) && $this->user->type == 2 )
			return true;
		else
			return false;
	}
	
	function renderContent()
	{		
		global $content;
		global $siteName;
			
		$fileContent = $this->_fileInfo["content"];
		$name = str_replace('"','&quot;',$this->_fileInfo['name']);
		$title = str_replace('"','&quot;',$this->_fileInfo['title']);
		$desc = str_replace('"','&quot;',$this->_fileInfo['desc']);
		$showInMenu = $this->_fileInfo['showInMenu'];
		
		$content = "
			<style>
			table * td { padding:0px; }
			a,a:link,a:active { background-color: transparent;}
			form div, form p { margin:0px;}
			table { border:solid 0px transparent;}
			body, p, td
			{
				font-family: verdana;
				font-size:12px;
			}
			</style>
			<p><h1>Edit page: $this->_pageId</h1></p>
			<form method=\"POST\" action=\"index.php?pg=edit_page&pageId=".$this->_pageId."&act=save\">
			<fieldset style='width:440px;padding:20px;'>
				<legend>Article details:</legend>
				name: <input type='text' name='txtName' id='txtName' size='80' value=\"".$name."\"/><br />
				description: <input type='text' name='txtDesc' id='txtDesc' size='80' value=\"".$desc."\"/><br />
				title: <input type='text' name='txtTitle' id='txtTitle' size='80' value=\"".$title."\"/><br />
				<input type='checkbox' name='chkShowInMenu' id='chkShowInMenu' ".($showInMenu == "on" ? "checked":"").">show in menu</input><br/><br/>
			</fieldset>
			<script type=\"text/javascript\" src=\"./modules/ext/tiny_mce/tiny_mce.js\"></script>
			<script type=\"text/javascript\">
			tinyMCE.init({
				theme : \"advanced\",
				mode : \"textareas\",
				plugins : \"fullscreen,advimage,advlink,searchreplace,table,contextmenu,paste\",
				theme_advanced_buttons3_add : \"fullscreen,search,replace,tablecontrols,pastetext,pasteword,selectall\",
				fullscreen_new_window : true,
				fullscreen_settings : {
					theme_advanced_path_location : \"top\"
				},
				table_styles : \"Header 1=header1;Header 2=header2;Header 3=header3\",
				table_cell_styles : \"Header 1=header1;Header 2=header2;Header 3=header3;Table Cell=tableCel1\",
				table_row_styles : \"Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1\",
				table_cell_limit : 100,
				table_row_limit : 5,
				table_col_limit : 5,
			   paste_auto_cleanup_on_paste : true,
				paste_preprocess : function(pl, o) {
					alert(o.content);
				},
				paste_postprocess : function(pl, o) {
					alert(o.node.innerHTML);
				}				
			});

			</script>
			<textarea name=\"txtPageContent\" id=\"txtPageContent\" style=\"width:540px;height:440px;\">".$fileContent."</textarea>
			<input type=\"submit\" value='Save'>
			</form>";

		if($this->action == "save" && $this->_success)
			$content .= "<P/><label style='color:green;'>Page saved successfully</label>";			
		else if ($this->action == "save" && !$this->_success)
			$content .= "<P/><label style='color:red;'>Failed to save page</label>";
	}
}

$renderer = new EditPageRenderer();

?>
